package com.ldk.framework.service.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ldk.base.framework.common.CustomException;
import com.ldk.base.framework.dao.IBaseDAO;
import com.ldk.base.framework.service.impl.BaseService;
import com.ldk.framework.service.IMenuService;
import com.ldk.framework.service.IRoleMenuService;
import com.ldk.framework.dao.IMenuDAO;
import com.ldk.framework.entity.Menu;
import com.ldk.framework.entity.RoleMenu;

/**
* MenuService接口实现类
* @author xiangwl
*/
@Service
public class MenuService extends BaseService<Menu> implements IMenuService {

	@Autowired
	private IMenuDAO menuDAO;
	@Autowired
	private IRoleMenuService roleMenuService;

	@Override
	protected IBaseDAO<Menu> getBaseDao() {
     	return this.menuDAO;
	}

	@Override
	public List<Menu> getUserMenus(String dbKey, int roleId) {
		return menuDAO.getUserMenus(dbKey, roleId);
	}

	@Override
	public List<Menu> getRoleMenus(String dbKey, int roleId) {
		return menuDAO.getRoleMenus(dbKey, roleId);
	}

	@Override
	@Transactional(rollbackFor={Exception.class, CustomException.class, NullPointerException.class, SQLException.class, IOException.class})
	public Boolean deleteMenu(String dbKey, int menuId) throws Exception {
		//删除当前ID的菜单
		Menu query = new Menu();
		query.setMenuId(menuId);
		int row = menuDAO.deleteByKey(dbKey, query);
		if(row < 1)
			throw new CustomException("删除菜单失败");
		//删除该菜单的所有子菜单
		query = new Menu();
		query.setParentId(menuId);
		row = menuDAO.delete(dbKey, query);
		//删除该菜单被配置的角色
		RoleMenu roleQuery = new RoleMenu();
		roleQuery.setMenuId(menuId);
		roleMenuService.delete(dbKey, roleQuery);
		//删除成功	 
		return true;
	}

	@Override
	public synchronized Boolean addMenu(String dbKey, Menu entity) {
		if(entity.getSortOrder() == null || entity.getSortOrder() < 1){
			int parentId = entity.getParentId();
			Menu query = new Menu();
			query.setMenuId(parentId);
			
			Menu parentMenu = selectByKey(dbKey, query);
			if(parentMenu == null || parentMenu.getSortOrder() < 0){
				entity.setSortOrder(1);
			}
			else{
				entity.setSortOrder(parentMenu.getSortOrder() + 1);
			}
		}
		if(entity.getEnabled() == null)
			entity.setEnabled(false);
		if(entity.getParentId() == null || entity.getParentId() < 0)
			entity.setParentId(0);
	    int row = insert(dbKey, entity);
	    if(row > 0){
	    	return true;
	    }
	    return false;
	}

	@Override
	public Boolean updateMenu(String dbKey, Menu entity) {
		if(entity.getEnabled() == null)
			entity.setEnabled(false);
		if(entity.getParentId() == null || entity.getParentId() < 0)
			entity.setParentId(0);
		if(entity.getSortOrder() == null || entity.getSortOrder() < 1){
			int parentId = entity.getParentId();
			Menu query = new Menu();
			query.setMenuId(parentId);
			Menu parentMenu = selectByKey(dbKey, query);
			if(parentMenu == null || parentMenu.getSortOrder() < 0){
				entity.setSortOrder(1);
			}
			else{
				entity.setSortOrder(parentMenu.getSortOrder() + 1);
			}
		}
		int row = updateByKey(dbKey, entity);
	    if(row > 0){
	    	return true;
	    }
		return false;
	}

}
